<?php

    namespace App\Admin\Controllers;

    use App\Admin\Grid\NGrid;
    use App\Admin\Tools\Audit\IncomeAuditTool;
    use App\Models\ContractedCustomerModel;
    use App\Models\FinanceIncomeModel;
    use Encore\Admin\Controllers\AdminController;
    use Encore\Admin\Facades\Admin;
    use Encore\Admin\Layout\Content;
    use Encore\Admin\Widgets\Tab;
    use Encore\Admin\Grid;
    use Encore\Admin\Grid\Displayers\Actions;
    use Encore\Admin\Grid\Tools;
    use Encore\Admin\Widgets\Table;
    use Illuminate\Support\Facades\Auth;

    class AuditIncomeController extends AdminController
    {
        protected $title = '财务收入审核';

        public function index(Content $content)
        {
            return Admin::content(function (Content $content) {
                $content->header($this->title);
                $content->description('列表');
                $content->body($this->grid());
            });
        }

        public function grid()
        {
            $tab = new Tab();
            $tab->add('待审核', $this->notAudit());
            $tab->add('已通过', $this->auditSuccess());
            $tab->add('已拒绝', $this->auditFailed());
            return $tab->render();
        }

        public function notAudit(): NGrid
        {
            $grid = new NGrid(new FinanceIncomeModel);
            $grid->model()
                ->leftJoin('contracted_customer', 'contracted_customer.id', 'finance_income.contracted_customer_id')
                ->leftJoin('admin_users', 'admin_users.id', 'contracted_customer.operate_id')
                ->where('finance_income.status', 0)
                ->select([
                    'finance_income.*',
                    'admin_users.name as operate_name',
                    'contracted_customer.male_name as male_name',
                    'contracted_customer.female_name as female_name',
                    'contract_no',
                    'contract_amount',
                ]);
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('contracted_customer_id', '客户信息')->select((new ContractedCustomerModel)->customerSelect());
                    $filter->date('income_date', __('finance_income.incomeDate'));
                });
            });

//            if (!ISADMIN) {
//                if (ISMAIN) $grid->model()->where('finance_income.main_id', USERID);
//                else $grid->model()->where('finance_income.main_id', MAINID);
//            }else $grid->model()->where('finance_income.operate_id', USERID);

            if (!ISADMIN) {
                if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('finance_income.main_id', MAINID);
                else $grid->model()->where('finance_income.operate_id', USERID);
            }
            if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
            $grid->column('income_type', __('finance_income.incomeType'));
            $grid->column('customer_info', '客户信息')->display(function () {
                return '合同编号：'.$this->contract_no.';男方姓名：'
                    .$this->male_name.';女方姓名：'.$this->female_name.';合同金额：'.$this->contract_amount;
            });
            $grid->column('income_amount', __('finance_income.incomeAmount'));
            $grid->column('actual_income_amount', __('finance_income.actualIncomeAmount'));
            $grid->column('income_date', __('finance_income.incomeDate'));
            $grid->column('remark', __('l.remark'));
            $grid->column('status', __('l.status'))->using(__('l.auditStatusArr'));
            $grid->column('audit_remark', __('finance_income.auditRemark'));
            $grid->column('created_at', __('l.createdAt'))->display(function() {
                return date('Y-m-d H:i:s', strtotime($this->created_at));
            });
            $grid->actions(function (Actions $action) {
                $action->disableEdit();
                $action->disableDelete();
                $action->disableView();
                if((int)$action->row->status === 0) $action->append(new IncomeAuditTool($action->row->id));
            });
            $grid->disableActions(false);
            $grid->disableCreateButton();
            return $grid;
        }

        public function auditSuccess(): NGrid
        {
            $grid = new NGrid(new FinanceIncomeModel);
            $grid->model()
                ->leftJoin('contracted_customer', 'contracted_customer.id', 'finance_income.contracted_customer_id')
                ->leftJoin('admin_users', 'admin_users.id', 'contracted_customer.operate_id')
                ->where('finance_income.status', 1)
                ->select([
                    'finance_income.*',
                    'admin_users.name as operate_name',
                    'contracted_customer.male_name as male_name',
                    'contracted_customer.female_name as female_name',
                    'contract_no',
                    'contract_amount',
                ]);
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('contracted_customer_id', '客户信息')->select((new ContractedCustomerModel)->customerSelect());
                    $filter->date('income_date', __('finance_income.incomeDate'));
                });
            });
            if (!ISADMIN) {
                if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('finance_income.main_id', MAINID);
                else $grid->model()->where('finance_income.operate_id', USERID);
            }
            if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
            $grid->column('income_type', __('finance_income.incomeType'));
            $grid->column('customer_info', '客户信息')->display(function () {
                return '合同编号：'.$this->contract_no.';男方姓名：'
                    .$this->male_name.';女方姓名：'.$this->female_name.';合同金额：'.$this->contract_amount;
            });
            $grid->column('income_amount', __('finance_income.incomeAmount'));
            $grid->column('actual_income_amount', __('finance_income.actualIncomeAmount'));
            $grid->column('income_date', __('finance_income.incomeDate'));
            $grid->column('remark', __('l.remark'));
            $grid->column('status', __('l.status'))->using(__('l.auditStatusArr'));
            $grid->column('audit_remark', __('finance_income.auditRemark'));
            $grid->column('created_at', __('l.createdAt'))->display(function() {
                return date('Y-m-d H:i:s', strtotime($this->created_at));
            });
            $grid->disableActions();
            $grid->disableCreateButton();
            return $grid;
        }

        public function auditFailed(): NGrid
        {
            $grid = new NGrid(new FinanceIncomeModel);
            $grid->model()
                ->leftJoin('contracted_customer', 'contracted_customer.id', 'finance_income.contracted_customer_id')
                ->leftJoin('admin_users', 'admin_users.id', 'contracted_customer.operate_id')
                ->where('finance_income.status',2)
                ->select([
                    'finance_income.*',
                    'admin_users.name as operate_name',
                    'contracted_customer.male_name as male_name',
                    'contracted_customer.female_name as female_name',
                    'contract_no',
                    'contract_amount',
                ]);
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                    $filter->equal('contracted_customer_id', '客户信息')->select((new ContractedCustomerModel)->customerSelect());
                    $filter->date('income_date', __('finance_income.incomeDate'));
                });
            });
            if (!ISADMIN) {
                if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('finance_income.main_id', MAINID);
                else $grid->model()->where('finance_income.operate_id', USERID);
            }
            if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
            $grid->column('income_type', __('finance_income.incomeType'));
            $grid->column('customer_info', '客户信息')->display(function () {
                return '合同编号：'.$this->contract_no.';男方姓名：'
                    .$this->male_name.';女方姓名：'.$this->female_name.';合同金额：'.$this->contract_amount;
            });
            $grid->column('income_amount', __('finance_income.incomeAmount'));
            $grid->column('actual_income_amount', __('finance_income.actualIncomeAmount'));
            $grid->column('income_date', __('finance_income.incomeDate'));
            $grid->column('remark', __('l.remark'));
            $grid->column('status', __('l.status'))->using(__('l.auditStatusArr'));
            $grid->column('audit_remark', __('finance_income.auditRemark'));
            $grid->column('created_at', __('l.createdAt'))->display(function() {
                return date('Y-m-d H:i:s', strtotime($this->created_at));
            });
            $grid->disableActions();
            $grid->disableCreateButton();
            return $grid;
        }
    }
